package com.hxd.otc.module.my.presenter;

import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.bean.ImageCodeBean;
import com.hxd.otc.module.my.contract.ChangePhoneContract;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;

/**
 * Created by lzy on 2018/6/15 0015.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/6/15 0015
 * description ${TODO}
 */
public class ChangePhonePresenter extends BasePresenter<ChangePhoneContract.View> implements ChangePhoneContract.Presenter {

    private final UserModel mUserModel;

    public ChangePhonePresenter(ChangePhoneContract.View view) {
        super(view);
        mUserModel = UserModel.getInstance();
    }

    @Override
    public void getUpdatePhoneVerCode(String phone, String countryId, final boolean isOld) {
        mUserModel.getUpdatePhoneVerCode(phone, countryId, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.getUpdatePhoneVerCodeSuccess(msg, isOld);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.getUpdatePhoneVerCodeFail(errMessage, isOld);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    /**
     * 获取需要新绑定的手机号验证码
     */
    @Override
    public void getNewBoundPhoneVerCode(String phone) {
        mUserModel.sendMobileSms(phone, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.getNewPhoneVerCodeSuccess(msg);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.getNewPhoneVerCodeFail(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());

    }

    @Override
    public void findUser(final String phone) {
        mUserModel.findUser(phone, new BaseObserver<ImageCodeBean>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ImageCodeBean imageCodeBean) {
                mView.checkNewPhoneSuccess(phone, imageCodeBean);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.checkNewPhoneFail(errMessage);
            }
        }, mView.<BaseBean<ImageCodeBean>>bindAutoDispose());
    }

    @Override
    public void onBindPhone(final String phone, String verCode, String countryId) {
        mUserModel.bindPhone(phone, verCode, countryId, new BaseObserver<String>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull String s) {
                mView.conBindPhoneSuccess(phone, s);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<String>>bindAutoDispose());
    }

    @Override
    public void onModifyBoundPhone(final String newPhone, String oldCode, String newCode, String countryId) {
        mUserModel.updatePhone(newPhone, oldCode, newCode, countryId, new BaseObserver<String>() {
            @Override
            public void onNext(String msg, String o) {
                mView.onModifyBoundPhoneSuccess(newPhone, msg);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
            }

        }, mView.<BaseBean<String>>bindAutoDispose());
    }
}
